const { ref } = Vue
import { useFetch } from '../../../Scripts/hooks/useFetch.js'
const { ElMessage } = ElementPlus

export function useQtyChangeBlur() {
  // 校验【自检合格数】
  const checkSelfCheckPassQty = configObj => {
    const { reportDataInfo, containerInfo, childContainerTableRef, childContainerList } = configObj;
    if (!reportDataInfo.selfCheckPassQty) {
      reportDataInfo.selfCheckPassQty = 0;
    }
    let reg = /^[0-9]+?$/;
    if (!reg.test(reportDataInfo.selfCheckPassQty)) {
      reportDataInfo.selfCheckPassQty = 0;
      return ElMessage({ type: 'error', message: `自检合格数必须是整数`, grouping: true });
    }
    // 单件带子序号
    if (containerInfo.containertype === 3) {
      childContainerTableRef.value.clearSelection();//先清空表格
      // 报工数 = 自检合格数 + 自检超差数 + 自检报废数
      const reportQty = parseInt(reportDataInfo.selfCheckPassQty) + parseInt(reportDataInfo.selfCheckDefeatQty) + parseInt(reportDataInfo.selfCheckLossQty);
      // 剩余可报数 = 在制数 - （已报工数 - 已报废数）
      const restQty = parseInt(containerInfo.wipqty) - (parseInt(containerInfo.reportedQty) - containerInfo.lossedQty);
      if (reportQty > restQty) {
        reportDataInfo.selfCheckPassQty = 0;
        return ElMessage({ type: 'error', message: `报工数不能大于${restQty}`, grouping: true });
      }
      // 过滤索引大于可报数
      const selectRows = childContainerList.value.filter((i, index) => index < reportQty);
      selectRows.forEach(row => { childContainerTableRef.value.toggleRowSelection(row, undefined) });
    }
  }

  // 校验【自检超差数】
  const checkSelfCheckDefeatQty = configObj => {
    const { reportDataInfo } = configObj;
    if (!reportDataInfo.selfCheckDefeatQty) {
      reportDataInfo.selfCheckDefeatQty = 0;
    }
    let reg = /^[0-9]+?$/;
    if (!reg.test(reportDataInfo.selfCheckDefeatQty)) {
      ElMessage({ type: 'error', message: `自检超差数必须是整数`, grouping: true });
      reportDataInfo.selfCheckDefeatQty = 0;
    }
  }

  // 校验【自检报废数】
  const checkSelfCheckLossQty = configObj => {
    const { reportDataInfo } = configObj;
    if (!reportDataInfo.selfCheckLossQty) {
      reportDataInfo.selfCheckLossQty = 0;
    }
    let reg = /^[0-9]+?$/;
    if (!reg.test(reportDataInfo.selfCheckLossQty)) {
      ElMessage({ type: 'error', message: `自检超差数必须是整数`, grouping: true });
      reportDataInfo.selfCheckLossQty = 0;
    }
  }

  // 校验【试件数】
  const checkINMTQty = configObj => {
    const { reportDataInfo } = configObj;
    if (!reportDataInfo.INMTQty) {
      reportDataInfo.INMTQty = 0;
    }
    let reg = /^[0-9]+?$/;
    if (!reg.test(reportDataInfo.INMTQty)) {
      reportDataInfo.INMTQty = 0;
      return ElMessage({ type: 'error', message: `试件数必须是整数`, grouping: true });
    }
  }

  // 校验【试件废票数】
  const checkINMTLossQty = configObj => {
    const { reportDataInfo, containerInfo } = configObj;

    if (!reportDataInfo.INMTLossQty) {
      reportDataInfo.INMTLossQty = 0;
    }
    let reg = /^[0-9]+?$/;
    if (!reg.test(reportDataInfo.INMTLossQty)) {
      reportDataInfo.INMTLossQty = 0;
      return ElMessage({ type: 'error', message: `试件废票数必须是整数`, grouping: true });
    }
    if (parseInt(reportDataInfo.INMTLossQty) > parseInt(reportDataInfo.INMTQty)) {
      reportDataInfo.INMTLossQty = 0;
      return ElMessage({ type: 'error', message: `试件废票数不能大于试件数`, grouping: true });
    }
    if (parseInt(reportDataInfo.INMTLossQty) > parseInt(containerInfo.wipqty)) {
      reportDataInfo.INMTLossQty = 0;
      return ElMessage({ type: 'error', message: `试件废票数不能大于批次在制数`, grouping: true });
    }
  }

  // 结余件确认扫卡
  const setSurPlusEmployeeInfo = async configObj => {
    const { reportDataInfo, userInfo, baseAPI } = configObj;

    if (!reportDataInfo.surPlusEmployeeCardNo) {
      return;
    }

    const cardnum = reportDataInfo.surPlusEmployeeCardNo;

    reportDataInfo.surPlusEmployeeID = '';
    reportDataInfo.surPlusEmployeeCardNo = '';
    reportDataInfo.surPlusEmployeeName = '';

    const { data } = await useFetch({
      url: `${baseAPI}?actions=getEmployeeByCardNum`,
      data: { type: 'getEmployeeByCardNum', factoryid: userInfo.value.factoryid, cardnum }
    });

    if (data.value.length === 0) { return ElMessage({ type: 'error', message: `所扫卡的用户不存在`, grouping: true }); }
    const { rolenames, fullname, employeeid } = data.value[0];
    if (!rolenames.includes('工区长') && !rolenames.includes('班组长')) {
      return ElMessage({ type: 'error', message: `所扫卡的用户没有工区长或班组长角色`, grouping: true });
    }
    reportDataInfo.surPlusEmployeeName = fullname;
    reportDataInfo.surPlusEmployeeID = employeeid;
  }

  return {
    checkSelfCheckPassQty, checkSelfCheckDefeatQty, checkSelfCheckLossQty, checkINMTQty, checkINMTLossQty, setSurPlusEmployeeInfo
  }
}